package com.example.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;


    public User UserRegister(String email, String password) {
        User newuser = new User();
        newuser.setEmail(email);
        newuser.setPassword(password);
        newuser.setUser_state(true);
        userMapper.insert(newuser);
        return newuser;
    }

    public User isUserExist(String email) {
        System.out.println(email);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        return userMapper.selectOne(queryWrapper);
    }

    public User isLogin(String email, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email).eq("password", password);
        return userMapper.selectOne(queryWrapper);
    }

    public User UserLogin(String email, String password) {
        return isLogin(email, password);
    }

    public User findUserById(Integer user_id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        return userMapper.selectOne(queryWrapper.eq("user_id", user_id));
    }

    public List<User> findAllUser() {
        return userMapper.selectList(null);
    }

    public User updatePassword(Integer userId, String encrypt) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId);
        updateWrapper.set("password", encrypt);
        userMapper.update(null, updateWrapper);
        return userMapper.selectOne(queryWrapper.eq("user_id", userId));
    }

    public User updateAddress(Integer userId, String address) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId);
        updateWrapper.set("address", address);
        userMapper.update(null, updateWrapper);
        return userMapper.selectOne(queryWrapper.eq("user_id", userId));
    }

    public User updateUsername(Integer userId, String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId);
        updateWrapper.set("username", username);
        userMapper.update(null, updateWrapper);
        return userMapper.selectOne(queryWrapper.eq("user_id", userId));
    }

    public User updateAvatar(Integer userId, String body) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId);
        updateWrapper.set("avatar", body);
        userMapper.update(null, updateWrapper);
        return userMapper.selectOne(queryWrapper.eq("user_id", userId));
    }

    public List<User> findUserSearch(String searchContent) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        return userMapper.selectList(queryWrapper.like("username", searchContent).or().like("user_id", searchContent).or().like("email", searchContent));
    }

    public User updateStatus(Integer userId, Boolean status) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId);
        updateWrapper.set("user_state", status);
        userMapper.update(null, updateWrapper);
        return userMapper.selectOne(queryWrapper.eq("user_id", userId));
    }

    public void deleteUser(Integer userId) {
        userMapper.deleteById(userId);
        System.out.println("删除成功");

    }
}
